import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class LoadingDialog extends Dialog {
  String tipMsg;

  LoadingDialog({this.canceledOnTouchOutside = true, this.tipMsg}) : super();

  ///点击背景是否能够退出
  final bool canceledOnTouchOutside;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: new Material(
          ///背景透明
          color: Colors.transparent,

          ///保证控件居中效果
          child: Stack(
            children: <Widget>[
              GestureDetector(
                ///点击事件
                onTap: () {
                  if (canceledOnTouchOutside) {
                    Navigator.pop(context);
                  }
                },
              ),
              _dialog(context)
            ],
          )),
    );
  }

  Widget _dialog(BuildContext context) {
    return new Center(
      ///弹框大小
      child: new SizedBox(
        width: ScreenUtil().setWidth(155),
        height: ScreenUtil().setHeight(155),
        child: new Container(
          ///弹框背景和圆角
          decoration: ShapeDecoration(
            color: Colors.black,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(
                Radius.circular(20),
              ),
            ),
          ),
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              _buildeCircularProgress(context),
              _BuildeText(context),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildeCircularProgress(BuildContext context) {
    return new CircularProgressIndicator(
      valueColor: new AlwaysStoppedAnimation(Colors.white),
    );
  }

  Widget _BuildeText(BuildContext context) {
    String tiptxt = this.tipMsg == null ? '加载中' : tipMsg;
    return Padding(
      padding: const EdgeInsets.only(
        top: 20.0,
      ),
      child: new Text(
        '${tiptxt}',
        style: new TextStyle(fontSize: 16.0, color: Colors.white),
      ),
    );
  }
}
